home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / lists.arc / LISTS.PRO
Encoding:
Prolog Source  |  1986-06-19  |  5.9 KB  |  184 lines

  1. /*   These are the basic predicates for list 
  2.      manipulation as defined by section 7.5,
  3.      "Programming in Prolog" by Clocksin and
  4.      Mellish.  Since the predicates defined in
  5.      the above referenced book do not function
  6.      under TURBO PROLOG, some minor and major
  7.      modifications had to be made.  If you 
  8.      should happend to find an easier way to
  9.      perform these predicates, please drop
  10.      a copy of your source on the bulletin
  11.      board.  Thanx.
  12.      
  13.          A. J. La Rocque
  14.          
  15. */         
  16.  
  17.  
  18. domains
  19.  
  20.     number    = integer
  21.     character = char
  22.     word      = string
  23.     attribute = symbol
  24.     
  25.     list_of_numbers     = number*
  26.     list_of_characters     = character*
  27.     list_of_words         = word*
  28.     list_of_attributes     = symbol*
  29.     
  30.     
  31. predicates
  32.  
  33.     addlist(attribute,list_of_attributes,list_of_attributes).
  34.     addlist(character,list_of_characters,list_of_characters).
  35.     addlist(number,list_of_numbers,list_of_numbers).
  36.     addlist(word,list_of_words,list_of_words).
  37.  
  38.     append(list_of_attributes,list_of_attributes,list_of_attributes).
  39.     append(list_of_characters,list_of_characters,list_of_characters).
  40.     append(list_of_numbers,list_of_numbers,list_of_numbers).
  41.     append(list_of_words,list_of_words,list_of_words).
  42.  
  43.     delete(attribute,list_of_attributes,list_of_attributes).
  44.     delete(character,list_of_characters,list_of_characters).
  45.     delete(number,list_of_numbers,list_of_numbers).
  46.     delete(word,list_of_words,list_of_words).
  47.  
  48.     efface(attribute,list_of_attributes,list_of_attributes).
  49.     efface(character,list_of_characters,list_of_characters).
  50.     efface(number,list_of_numbers,list_of_numbers).
  51.     efface(word,list_of_words,list_of_words).
  52.  
  53.     head(list_of_attributes,attribute).
  54.     head(list_of_characters,character).
  55.     head(list_of_numbers,number).    
  56.     head(list_of_words,word).
  57.  
  58.     intersection(list_of_attributes,list_of_attributes,list_of_attributes).
  59.     intersection(list_of_characters,list_of_characters,list_of_characters).
  60.     intersection(list_of_numbers,list_of_numbers,list_of_numbers).
  61.     intersection(list_of_words,list_of_words,list_of_words).
  62.  
  63.     last(attribute,list_of_attributes).
  64.     last(character,list_of_characters).
  65.     last(number,list_of_numbers).
  66.     last(word,list_of_words).
  67.  
  68.     member(attribute,list_of_attributes).
  69.     member(character,list_of_characters).
  70.     member(number,list_of_numbers).
  71.     member(word,list_of_words).
  72.  
  73.     nextto(attribute,attribute,list_of_attributes).
  74.     nextto(character,character,list_of_characters).
  75.     nextto(number,number,list_of_numbers).
  76.     nextto(word,word,list_of_words).
  77.  
  78.     prefix(list_of_attributes,list_of_attributes).
  79.     prefix(list_of_characters,list_of_characters).
  80.     prefix(list_of_numbers,list_of_numbers).
  81.     prefix(list_of_words,list_of_words).
  82.  
  83.     reverse(list_of_attributes,list_of_attributes).
  84.     reverse(list_of_characters,list_of_characters).
  85.     reverse(list_of_numbers,list_of_numbers).
  86.     reverse(list_of_words,list_of_words).
  87.  
  88.     sublist(list_of_attributes,list_of_attributes).
  89.     sublist(list_of_characters,list_of_characters).
  90.     sublist(list_of_numbers,list_of_numbers).
  91.     sublist(list_of_words,list_of_words).
  92.  
  93.     subset(list_of_attributes,list_of_attributes).
  94.     subset(list_of_characters,list_of_characters).
  95.     subset(list_of_numbers,list_of_numbers).
  96.     subset(list_of_words,list_of_words).
  97.  
  98.     subst(attribute,list_of_attributes,attribute,list_of_attributes).
  99.     subst(character,list_of_characters,character,list_of_characters).
  100.     subst(number,list_of_numbers,number,list_of_numbers).
  101.     subst(word,list_of_words,word,list_of_words).
  102.  
  103.     tail(list_of_attributes,list_of_attributes).
  104.     tail(list_of_characters,list_of_characters).
  105.     tail(list_of_numbers,list_of_numbers).    
  106.     tail(list_of_words,list_of_words).
  107.  
  108.     union(list_of_attributes,list_of_attributes,list_of_attributes).
  109.     union(list_of_characters,list_of_characters,list_of_characters).
  110.     union(list_of_numbers,list_of_numbers,list_of_numbers).
  111.     union(list_of_words,list_of_words,list_of_words).
  112.  
  113. clauses
  114.  
  115.     /*
  116.         Finding the last element of a list:  the goal
  117.         last(X,L) succeeds if element X is the last
  118.         element of list L.  The boundary condition is
  119.         when there is only one element in L.
  120.     */
  121.     
  122.     last(X,[Y]) :- X = Y,!.
  123.     last(X,[_|Y]) :- last(X,Y).
  124.     
  125.     
  126.     append([],L,L).
  127.     append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
  128.             
  129.     reverse([],[]).
  130.     reverse([Head|Tail],List) :- reverse(Tail,Z),
  131.                      append(Z,[Head],List).
  132.                      
  133.     efface(A,[X|L],L) :- A = X,!.
  134.     efface(A,[B|L],[B|M]) :- efface(A,L,M).
  135.                      
  136.     delete(_,[],[]).
  137.     delete(X,[Y|L],M) :- X = Y,!, delete(X,L,M).
  138.     delete(X,[Y|L1],[Y|L2]) :- delete(X,L1,L2).
  139.     
  140.     subst(_,[],_,[]).
  141.     subst(X,[Y|L],A,[B|M]) :- X = Y and A = B,!,subst(X,L,A,M).
  142.     subst(X,[Y|L],A,[Y|M]) :- subst(X,L,A,M).
  143.     
  144.     sublist([X|L],[X|M]) :- prefix(L,M),!.
  145.     sublist(L,[_|M]) :- sublist(L,M).
  146.     
  147.     prefix([],_).
  148.     prefix([X|L],[X|M]) :- prefix(L,M).
  149.     
  150.     member(X,[Y|_]) :- X = Y.
  151.     member(X,[_|Y]) :- member(X,Y).
  152.     
  153.     subset(X,Y) :- head(X,A), member(A,Y),!, efface(A,X,Z), subset(Z,Y).
  154.     subset([],_).
  155.         
  156.     tail([_|X],Y) :- X = Y.
  157.     
  158.     head([X|_],Y) :- Y = X.
  159.  
  160.     nextto(X,Y,L) :- head(L,A),
  161.                  A = X,
  162.                  tail(L,B),
  163.              head(B,C),
  164.              C = Y.                 
  165.     nextto(X,Y,[_|L]) :- nextto(X,Y,L).
  166.                  
  167.  
  168.     intersection([],_,[]).
  169.  
  170.     intersection([X|A],B,[Y|C]) :-
  171.     head([X|A],H),
  172.     member(H,B),
  173.     !,
  174.     X = Y,
  175.     intersection(A,B,C).
  176.     intersection([_|R],Y,Z) :- intersection(R,Y,Z).
  177.                         
  178.     addlist(A,L1,[B|L1]) :- A = B.
  179.                 
  180.     union([],X,X).
  181.     union([X|R],Y,Z) :- head([X|R],H), member(H,Y), !, union(R,Y,Z).
  182.     union([X|R],Y,[X|Z]) :- union(R,Y,Z).
  183.                        
  184.